<?php

namespace app\admin\controller;
class Setview extends base
{
    //文件目录
    private $dir;
    //文件路径
    private $fileroot;


    public function _initialize()
    {
        //读取模板目录
//         $this->dir = APP_PATH . $this->request->param('type').'/view/';
//        $this->fileroot = $this->dir . $this->request->param('dir') . '/' . $this->request->param('name');

        $this->dir = APP_PATH . '../template/pc/' ;
        $this->fileroot =  $this->dir . $this->request->param('dir') .$this->request->param('name');;
    }

    /**
     * 读取文件目录
     * @param $basedir 文件地址
     */
    function checkdir($basedir)
    {
        static $filename = array();
        //上级目录名
        static $dir;
        if ($dh = opendir($basedir)) {
            while (($file = readdir($dh)) !== false) {

                if ($file != '.' && $file != '..') {
                    if (!is_dir($basedir . "/" . $file)) {
                        $filename[] = ['filename' => "$dir/$file", 'type'=>$this->request->param('type'),'dir' => "$dir", 'name' => $file];
                        //"filename: $dir/$file <br>";
                    } else {
                        $dir = $file;
                        $dirname = $basedir . "/" . $file;
                        $this->checkdir($dirname);
                    }
                }
            }
        }
        return $filename;
    }


    public function index()
    {
        //全模板路径
        //APP_PATH.request()->module().'/view/'.$this->request->controller().'_'.$this->request->action().'.'.config('template.view_suffix')
        $html = '';
        if ($this->request->param('name')) {
            $fp = fopen($this->fileroot, 'r') or die("无法打开文件！");
            if (filesize($this->fileroot)) {
                $html = fread($fp, filesize($this->fileroot));
                fclose($fp);
            }
            $this->assign('value', $html ? $html : '');
        }

        $dir = $this->checkdir($this->dir);
        $this->assign('dir', $dir);
        $this->assign('type', $this->request->param('type'));
        $this->assign('dirfile', $this->request->param('name'));

        return $this->fetch();
    }


    public function index2222()
    {
        //全模板路径
        //APP_PATH.request()->module().'/view/'.$this->request->controller().'_'.$this->request->action().'.'.config('template.view_suffix')
        $html = '';
        if ($this->request->param('dir') && $this->request->param('name')) {
            $fp = fopen($this->fileroot, 'r') or die("无法打开文件！");
            if (filesize($this->fileroot)) {
                $html = fread($fp, filesize($this->fileroot));
                fclose($fp);
            }
            $this->assign('value', $html ? $html : '');
        }

        $dir = $this->checkdir($this->dir);
        $this->assign('dir', $dir);
        $this->assign('type', $this->request->param('type'));
        $this->assign('dirfile', $this->request->param('dir') . '/' . $this->request->param('name'));

        return $this->fetch();
    }

    /**
     * 保存
     */
    public function save()
    {
        if (!$this->request->isPost()||empty($this->request->post('filevalue'))) {
            $this->error('请求失败');
        }

        $myfile = fopen(rtrim($this->fileroot, '/'), "w") or die("无法打开文件！");
        $txt = html_entity_decode($this->request->post('filevalue'));
        fwrite($myfile, $txt);
        fclose($myfile);
        $this->success('修改模板成功！');
    }
}
